package images

import (
	"gin-sns/app/common/uploader"
	"gin-sns/app/ginframe/cfg"
	"gin-sns/app/ginframe/tools"

	"strings"
)

func HandleOssImageStyleAvatar(url string) string {
	config := cfg.Instance()

	if !uploader.IsEnabledOss() {
		return url
	}
	return HandleOssImageStyle(url, config.AppConf.Uploader.AliyunOss.StyleAvatar)
}

func HandleOssImageStyleDetail(url string) string {
	config := cfg.Instance()

	if !uploader.IsEnabledOss() {
		return url
	}
	return HandleOssImageStyle(url, config.AppConf.Uploader.AliyunOss.StyleDetail)
}

func HandleOssImageStyleSmall(url string) string {
	config := cfg.Instance()

	if !uploader.IsEnabledOss() {
		return url
	}
	return HandleOssImageStyle(url, config.AppConf.Uploader.AliyunOss.StyleSmall)
}

func HandleOssImageStylePreview(url string) string {
	config := cfg.Instance()

	if !uploader.IsEnabledOss() {
		return url
	}
	return HandleOssImageStyle(url, config.AppConf.Uploader.AliyunOss.StylePreview)
}

func HandleOssImageStyle(url, style string) string {
	config := cfg.Instance()
	if tools.IsBlank(style) || tools.IsBlank(url) {
		return url
	}
	if !uploader.IsOssImageUrl(url) {
		return url
	}
	sep := config.AppConf.Uploader.AliyunOss.StyleSplitter
	if tools.IsBlank(sep) {
		return url
	}
	return strings.Join([]string{url, style}, sep)
}
